package com.dcrzl.shcz.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 稻草人之恋
 * @version 1.0
 * @date 2020/12/24 11:02
 * <p>
 * 数据库查询数据封装工具类
 **/
@Slf4j
public class ResultUtils {
    /**
     * 删除操作状态封装
     *
     * @param i 数据库删除操作返回值
     * @return
     */
    public static Result<Integer> deleteResult(int i) {
        Result<Integer> result = new Result<>();
        if (i <= 0) {
            result.setSuccess(false).setMsg("删除失败").setCode(SysConstant.DELETE_FAIL_CODE_RES);
        } else {
            result.setSuccess(true).setMsg("删除成功").setCode(SysConstant.SUCCESS_CODE_RES);
        }
        return result;
    }

    /**
     * 逻辑删除操作状态封装
     *
     * @param i 数据库更新操作返回值
     * @return
     */
    public static Result<Integer> removeResult(int i) {
        Result<Integer> result = new Result<>();
        result.setData(i);
        if (i <= 0) {
            result.setSuccess(false).setMsg("移除失败").setCode(SysConstant.DELETE_FAIL_CODE_RES);
        } else {
            result.setSuccess(true).setMsg("移除成功").setCode(SysConstant.SUCCESS_CODE_RES);
        }
        return result;
    }

    /**
     * 添加数据操作状态封装
     *
     * @param i 数据库添加操作返回值
     * @return
     */
    public static Result<Integer> insertResult(int i) {
        Result<Integer> result = new Result<>();
        result.setData(i);
        if (i <= 0) {
            result.setSuccess(false).setMsg("添加失败").setCode(SysConstant.ADD_FAIL_CODE_RES);
        } else {
            result.setSuccess(true).setMsg("添加成功").setCode(SysConstant.SUCCESS_CODE_RES);
        }
        return result;
    }

    /**
     * 更新操作状态封装
     *
     * @param i 数据库更新操作返回值
     * @return
     */
    public static Result<Integer> updateResult(int i) {
        Result<Integer> result = new Result<>();
        result.setData(i);
        if (i <= 0) {
            result.setSuccess(false).setMsg("更新失败").setCode(SysConstant.UPDATE_FAIL_CODE_RES);
        } else {
            result.setSuccess(true).setMsg("更新成功").setCode(SysConstant.SUCCESS_CODE_RES);
        }
        return result;
    }

    /**
     * 查询数据封装
     * 当有数据则为此次查询操作为成功，空数据则为失败，code值统一为查询成功操作0
     *
     * @param t 数据库查询数据
     * @return
     */
    public static <T> Result<T> selectOneResult(T t) {
        Result<T> result = new Result<>();
        if (t != null) {
            result.setSuccess(true).setData(t).setCode(SysConstant.SUCCESS_CODE_RES);
        } else {
            result.setSuccess(false).setCode(SysConstant.SELECT_FAIL_CODE_RES);
        }
        return result;
    }

    public static <T> Result<List<T>> selectResult(List<T> list) {
        Result<List<T>> result = new Result<>();
        if (!CollectionUtils.isEmpty(list)) {
            if(list.get(0)==null){
                result.setSuccess(false).setCode(SysConstant.SELECT_FAIL_CODE_RES);
            }else{
                result.setSuccess(true).setData(list)
                        .setCode(SysConstant.SUCCESS_CODE_RES);
            }
        } else {
            result.setSuccess(false).setCode(SysConstant.SELECT_FAIL_CODE_RES);
        }
        return result;
    }

    public static Result<Long> selectCountResult(Long i) {
        Result<Long> result = new Result<>();
        if (i != null) {
            result.setSuccess(true).setData(i).setCode(SysConstant.SUCCESS_CODE_RES);
        } else {
            result.setSuccess(false).setCode(SysConstant.SELECT_FAIL_CODE_RES);
        }
        return result;
    }


}
